#!/bin/bash
source /etc/profile

# 可输入使其，如无输入值，取昨天的日期
if [ -n "$1" ]
then 
    do_date=$1
else
    do_date=`date -d "-1 day" +%F`
fi

sql="
-- 加载自定义UDF
drop function if exists max_continuous_day;
create function max_continuous_day as 'com.lagou.dw.hive.udf.MaxContinuousDay' using jar 'hdfs://linux121:9000/user/hive/jars/com.lagou.dw-1.0-SNAPSHOT-jar-with-dependencies.jar';

insert overwrite table dws.dws_member_start_last_7_day partition(dt='$do_date')
-- 获取过去7天所有的活跃用户，并计算出每个用户的最长连续活跃天数
select 
    device_id,
    concat_ws('|', collect_set(area)),
    concat_ws('|', collect_set(uid)),
    concat_ws('|', collect_set(app_v)),
    concat_ws('|', collect_set(event_type)),
    concat_ws('|', collect_set(os_type)),
    concat_ws('|', collect_set(channel)),
    concat_ws('|', collect_set(language)),
    concat_ws('|', collect_set(brand)),
    concat_ws('|', collect_set(dt)),
    -- 计算最长连续活跃天数
    max_continuous_day(collect_set(dt)) as max_continuous_day
from dws.dws_member_start_day
where dt >= date_sub('$do_date', 7)
    and dt <= '$do_date'
group by device_id;
"

hive -e "$sql"